$(function () {
  let id = localStorage.getItem("uid");
  $.ajax({
    url: "http://jx.xuzhixiang.top/ap/api/cart-list.php",
    type: "get",
    data: {
      id: id,
    },
    success: function (data) {
      let skr = data.data;
      let str = "";
      skr.forEach((item) => {
        str += `<div class="che3">
                <input type="checkbox" class="duo">
                <img src="${item.pimg}" alt="">
                <p>${item.pname}</p>
            <div class="che4">
                <span class="jian" data-id="${item.pid}">-</span>
                <input type="text" class="text" value="${item.pnum}" data-id="${
          item.pid
        }">
                <span class="jia" data-id="${item.pid}">+</span><br>
            </div>
                <p data-id="${item.pid}">${item.pprice}</p>
                <p data-id="${item.pid}"  class="danzong">${
          item.pprice * item.pnum
        }</p>
                <p data-id="${item.pid}" class="shan">X</p>
            </div>`;
      });
      $(".che2").html(str);
      geng();
      $(".che5")
        .find(".quan")
        .click(function () {
          let a = $(this).prop("checked");
          console.log(a);
          $(".che3").find(".duo").prop("cheked", a);
          geng();
        });
      function geng() {
        let tiaoshu = $(".che2").children().length;
        console.log($(".che2"));
        console.log(tiaoshu);
        let xuanzhong = $(".duo:checked").length;
        console.log(xuanzhong);
        if (xuanzhong != tiaoshu) {
          $(".che5").find(".quan").prop(".checked", false);
        } else {
          $(".che5").find(".quan").prop("checked", true);
        }
        if (tiaoshu == 0) {
          $(".che5").find(".quan").prop("checked", false);
        }
        let shuzong = 0;
        let zongjia = 0;
        $.each($(".duo:checked"), function (item, n) {
          shuzong += $(n).siblings().eq(2).find(".text").val() * 1;
          zongjia += $(n).siblings().eq(4).html() * 1;
        });
        $(".sum").html(shuzong);
        $(".money").html(zongjia);
      }
      $(".duo").click(function () {
        geng();
      });
      $(".jia").click(function () {
        let shangid = $(this).attr("data-id");
        let danxuan = $(this).siblings("input").val();
        let danjia = $(this).parent().siblings().eq(3).html();
        // console.log($(this).parent().siblings());
        danxuan++;
        $(this).siblings("input").val(danxuan);
        $(this)
          .parent()
          .siblings(".danzong")
          .html(danjia * danxuan);
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
          type: "get",
          data: { uid: id, pid: shangid, pnum: danxuan },
          success: function () {
            // console.log(danxuan);
          },
        });
        geng();
      });
      $(".jian").click(function () {
        let shangid = $(this).attr("data-id");
        let danxuan = $(this).siblings("input").val();
        let danjia = $(this).parent().siblings().eq(3).html();
        danxuan--;
        if (danxuan <= 1) {
          danxuan = 1;
        }
        $(this).siblings("input").val(danxuan);
        $(this)
          .parent()
          .siblings(".danzong")
          .html(danjia * danxuan);
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
          type: "get",
          data: { uid: id, pid: shangid, pnum: danxuan },
          success: function () {},
        });
        geng();
      });
      $(".shan").click(function () {
        let shanid = $(this).attr("data-id");
        let shanshang = $(this).parent();
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/cart-delete.php",
          type: "get",
          data: { uid: id, pid: shanid },
          success: function () {
            $(this).siblings(".duo").prop("checked", false);
            shanshang.remove();
            geng();
            console.log($(this));
          },
        });
      });
    },
  });
});
